﻿using Newtonsoft.Json.Linq;
using RuoVea.GoFastdfs.Common;
using RuoVea.GoFastdfs.Service.Inserface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RuoVea.GoFastdfs.Service
{
    public class IndexService : IIndexService
    {
        /// <summary>
        /// 获取状态
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public JObject getStatus(String data)
        {
            // 声明结果集
            JObject result = new JObject();
            // 声明30天文件大小数据容器
            List<String> dayFileSizeList = new List<String>();
            // 声明30天文件数量数据容器
            List<String> dayFileCountList = new List<String>();
            // 声明30天内日期容器
            List<String> dayNumList = new List<String>();

            JObject parseObj = data.ToObject<JObject>();

            var parseArray = parseObj.GetValue("Fs.FileStats").ToList();
            // 剩余空间
            var diskFreeSize = FileSizeUtil.GetLength((long)Convert.ToInt64(parseObj["Sys.DiskInfo"]?["free"]?.ToString()));
            result.Add("diskFreeSize", diskFreeSize);
            // 总空间
            result.Add("diskTotalSize", FileSizeUtil.GetLength((long)Convert.ToInt64(parseObj["Sys.DiskInfo"]?["total"]?.ToString())));
            //// 已使用空间
            result.Add("diskUsedSize", FileSizeUtil.GetLength((long)Convert.ToInt64(parseObj["Sys.DiskInfo"]?["used"]?.ToString())));
            //// 节点
            result.Add("inodesTotal", FileSizeUtil.GetLength((long)Convert.ToInt64(parseObj["Sys.DiskInfo"]?["inodesTotal"]?.ToString())));
            //// 节点
            result.Add("inodesUsed", FileSizeUtil.GetLength((long)Convert.ToInt64(parseObj["Sys.DiskInfo"]?["inodesUsed"]?.ToString())));
            //// 节点
            result.Add("inodesFree", FileSizeUtil.GetLength((long)Convert.ToInt64(parseObj["Sys.DiskInfo"]?["inodesFree"]?.ToString())));
            long dayFileSize = 0;
            long dayFileCount = 0;
            for (int i = 0; i < parseArray.Count(); i++)
            {
                var fileStats = parseArray[i];
                if (fileStats["date"]?.ToString().Equals("all") ?? false)
                {
                    // 获取总文件大小,数量 
                    result.Add("totalFileSize", FileSizeUtil.GetLength((long)Convert.ToInt64(fileStats["totalSize"]?.ToString())));
                    result.Add("totalFileCount", fileStats["fileCount"]?.ToString());
                }
                else
                {
                    try
                    {
                        long subDay = DateUtil.daysBetween(DateUtil.StrToDate(fileStats["date"]?.ToString(), "yyyyMMdd"),DateTime.Now, false);
                        if (subDay <= 30)
                        {
                            // 获取30天内文件大小,数量
                            dayFileSize += Convert.ToInt32((fileStats["totalSize"]?.ToString()));
                            dayFileCount += Convert.ToInt32((fileStats["fileCount"]?.ToString()));
                            dayFileSizeList.Add(FileSizeUtil.GetMBLength(Convert.ToInt32(fileStats["totalSize"])));
                            dayFileCountList.Add(fileStats["fileCount"]?.ToString());
                            dayNumList.Add(fileStats["date"].ToString().Substring(6) + "日");
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.StackTrace);
                    }
                }
            }
            result.Add("dayFileSize", FileSizeUtil.GetLength(dayFileSize));
            result.Add("dayFileCount", dayFileCount);
            result.Add("dayFileSizeList", dayFileSizeList.ToJsonString());
            result.Add("dayFileCountList", dayFileCountList.ToJsonString());
            result.Add("dayNumList", dayNumList.ToJsonString());
            return result;
        }
    }
}
